package com.ymatou.iapi.livebuyer.testcase;

import com.ymatou.iapi.livebuyer.parameter.SystemNotifyBean;
import com.ymatou.iapi.livebuyer.service.SystemNotifyCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.livevideo.parameter.StreamLiveNotifyBean;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;
import org.apache.http.client.ClientProtocolException;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 发送系统消息 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SystemNotify {
	private static SystemNotifyBean systemnotifyBean;
	private static SystemNotifyCall systemnotifyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送系统消息");
	}

	@Before
	public void caseUp() {
		systemnotifyBean = new SystemNotifyBean();
		systemnotifyCall = new SystemNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(systemnotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SystemNotify_001() {
		Logger.start(true, "发送系统消息成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			Integer liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			Integer uid = 1;
			systemnotifyBean.setLiveId(liveid);
			systemnotifyCall.setData(systemnotifyBean);
			systemnotifyCall.callService();
			Logger.verifyEquals("ok", systemnotifyCall.getReturnData(), "验证return");
			List<Map> infos = new LiveRoomWapper().selectSystemNotify(liveid).stream().filter(info -> {
				return (info.get("BizId").toString().equals("902"));
			}).collect(Collectors.toList());
			Logger.verifyEquals(1, infos.size(), "验证902通知条数");
			verifyPushStream(liveid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SystemNotify_002() {
		Logger.start(true, "重复调用不会重复发送消息");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			Integer liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			Integer uid = 1;
			systemnotifyBean.setLiveId(liveid);
			systemnotifyCall.setData(systemnotifyBean);
			systemnotifyCall.callService();
			
			systemnotifyCall.callService();
			Logger.verifyEquals("ok", systemnotifyCall.getReturnData(), "验证return");
			List<Map> infos = new LiveRoomWapper().selectSystemNotify(liveid).stream().filter(info -> {
				return (info.get("BizId").toString().equals("902"));
			}).collect(Collectors.toList());
			Logger.verifyEquals(1, infos.size(), "验证902通知条数");
			verifyPushStream(liveid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SystemNotify_003() {
		Logger.start(true, "上次发送手动对焦的消息距离当前超过了30分钟则发送消息提醒买手手动对焦");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			Integer liveid = livedto.getId();
			LiveRoomCallService.SellerLiveOnline(liveid, livedto.getUserId());
			StreamLiveNotifyBean notify = LiveRoomCallService.initStreamLiveNotifyBean(liveid, 1);
			LiveRoomCallService.LiveNotify(notify);
			verifyPushStream(liveid);
			Integer uid = 1;
			systemnotifyBean.setLiveId(liveid);
			systemnotifyCall.setData(systemnotifyBean);
			systemnotifyCall.callService();
			 new LiveRoomWapper().updateSystemNotifyCreateTime(liveid, YMTDateUtil.getDateBeforeOrNextSecond(-31*60));
			systemnotifyCall.callService();
			Logger.verifyEquals("ok", systemnotifyCall.getReturnData(), "验证return");
			List<Map> infos = new LiveRoomWapper().selectSystemNotify(liveid).stream().filter(info -> {
				return (info.get("BizId").toString().equals("902"));
			}).collect(Collectors.toList());
			Logger.verifyEquals(2, infos.size(), "验证902通知条数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	private void verifyPushStream(Integer liveId) throws ClientProtocolException, IOException, InterruptedException {
		Logger.comment("验证总线发送消息");
		MessageListBean MessageListBean = MqQueryCallService.initSteamSync(liveId);
		List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
		int redo=0;
		while(joa.size()==0&&redo<5){
			redo++;
			Thread.sleep(5000);
			joa = MqQueryCallService.GetMqMessage(MessageListBean);
		}
		//Logger.verifyEquals(1, joa.size(), "验证发送总线消息");
		Logger.verifyEquals(liveId.toString(),joa.get(0).get("LiveId").toString(), "验证liveId");
		Thread.sleep(5000);
		LiveRoom createLive = new LiveRoomWapper().selectLiveRoomById(liveId);
		Integer status = createLive.getLiveStatus();
		Logger.verifyEquals(2, status, "验证status为2");
	}
}